﻿Imports System.Resources
Imports System.Reflection

Public Class Language
    Private GlobalRM As New ResourceManager("WebSitemap_Localizer.Global", Assembly.GetExecutingAssembly())

    Private Sub Language_Load(sender As Object, e As EventArgs) Handles Me.Load
        If String.IsNullOrEmpty(My.Settings.Language) Then
            setRadioButton(Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName)
        Else
            FormTranslator.ChangeFormLanguage(Me, My.Settings.Language)
            setRadioButton(My.Settings.Language)
        End If
    End Sub

    Private Sub Language_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If Me.DialogResult <> Windows.Forms.DialogResult.OK Then
            If MessageBox.Show(GlobalRM.GetString("Sure"), GlobalRM.GetString("Title"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) = MsgBoxResult.No Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub setRadioButton(ByVal lang As String)
        Select Case lang
            Case "it"
                rbtnItalian.Checked = True
            Case "en"
                rbtnEnglish.Checked = True
            Case Else
                rbtnEnglish.Checked = True
        End Select
    End Sub

    Private Function getSelectedLanguage() As String
        Dim lang As String = "en"

        If rbtnEnglish.Checked Then
            lang = "en"
        ElseIf rbtnItalian.Checked Then
            lang = "it"
        End If

        Return lang
    End Function

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        My.Settings.Language = getSelectedLanguage()
        My.Settings.Save()
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

End Class